<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">GalleryGo</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
      /></a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                  <a-menu-item @click="userCenter(loginUserId)">
                    <UserOutlined />
                    个人中心
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else><a-button type="primary" href="/user/login">登录</a-button></div>
        </div></a-col
      >
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, UserOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { userLoginStore } from '@/stores/userLoginStore'
import { userLogoutUsingPost } from '@/api/user'
const loginUserStore = userLoginStore()
// 原始菜单项
const original = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: 'others',
    label: h('a', { href: 'www.baidu.com', target: '_blank' }, '百度'),
    title: '百度',
  },
]
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    //管理员才能看到admin开头的子菜单
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}
const items = computed(() => filterMenus(original))
const router = useRouter()
let loginUserId = loginUserStore.loginUser.id
/**
 *
 * 个人中心
 * @param param0 userCenter
 */
function userCenter(loginUserId: string) {
  router.push({
    path: '/user/center',
    query: { id: loginUserId },
  })
}
/* 路由跳转事件 */
function doMenuClick({ key }) {
  router.push({
    path: key,
  })
}
//监听路由变化，更新路由高亮
const current = ref<string[]>([])
router.afterEach((to, formItemProps, next) => {
  current.value = [to.path]
})

function doLogout() {
  userLogoutUsingPost().then((res) => {
    if (res.data.code == 200) {
      loginUserStore.setLoginUser({
        id: '',
        userAccount: '',
        userAvatar: '',
        userName: '',
        userProfile: '',
        userRole: '',
        createTime: '',
        editTime: '',
        updateTime: '',
      })
      router.push('/user/login')
    } else {
      message.error('退出登录失败' + res.data.message)
    }
  })
}
</script>
<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}
.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}
.logo {
  height: 48px;
}
</style>

